package com.raptor.trustworthy.task.task;

import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 统一job处理任务类
 *
 * @Author renhuan
 * @Date 2021-04-18 21:01
 **/
@Component
public class JobExchange implements Job {

  private static final Logger log = LoggerFactory.getLogger(JobExchange.class);

@Autowired
private RestTemplate restTemplate;

  @Override
  public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
    String sendMode = jobDataMap.getString("sendMode");
    String jobName = jobDataMap.getString("jobName");
    String serverUrl = jobDataMap.getString("serverUrl");
    String res = null;
    try {
      switch (sendMode.toUpperCase()){
        case "GET":
          res = restTemplate.getForObject(serverUrl, String.class);break;
        case "POST":
          Map param = new HashMap();
          res = restTemplate.postForObject(serverUrl, param, String.class);break;
      }
    } catch (Exception e) {
      log.error("restTemplate == > " + e.toString());
      throw new JobExecutionException(e.toString());
    } finally {
      System.out.println("++======> " + res);
    }

  }
}
